<?
	ini_set('error_reporting', E_ALL);
	ini_set('display_errors', 1);

	$db_host = 'localhost';
	$db_user = 'root';
	$db_pass = 'kscsq';
	$db_schema = 'moto_parser';

	header('Content-Type: text/html; charset=utf-8');	

	if (($link = mysql_connect($db_host, $db_user, $db_pass)) && mysql_select_db($db_schema))
	{
		mysql_set_charset('utf8', $link);

		if (!empty($_GET['parts_group']))
		{
			$query = "SELECT img FROM parts_group WHERE id = ".$_GET['parts_group']." LIMIT 1";
			$result = mysql_query($query);
			$img = mysql_result($result, 0, 'img');

			$query = "SELECT * FROM part WHERE parts_group = ".$_GET['parts_group'];
			$result = mysql_query($query);

			if (mysql_num_rows($result))
			{
				?>
					<img src="img/<?=$img;?>"/>
				<?
				echo "<table>";
				while ($row = mysql_fetch_assoc($result))
				{
					?>
						<tr>
							<td>
								<?=$row['pos']?>
							</td>
							<td>
								<?=$row['num']?>
							</td>
							<td>
								<?=$row['name']?>
							</td>
						<tr>
					<?
				}
				echo "</table>";
			}
			exit;
		}

		// model
		if (!empty($_GET['model']))
		{
			$query = "SELECT * FROM parts_group WHERE model = ".$_GET['model'];
			$result = mysql_query($query);

			if (mysql_num_rows($result))
			{
				echo "<table>";
				while ($row = mysql_fetch_assoc($result))
				{
					?>
						<tr>
							<td>
								<a href="catalog.php?parts_group=<?=$row['id']?>"><?=$row['name']?></a>	
							</td>
						<tr>
					<?
				}
				echo "</table>";
			}
			exit;
		}

		// models
		if (!empty($_GET['year']))
		{
			$query = "SELECT * FROM model WHERE year = ".$_GET['year'];
			$result = mysql_query($query);

			if (mysql_num_rows($result))
			{
				echo "<table>";
				while ($row = mysql_fetch_assoc($result))
				{
					?>
						<tr>
							<td>
								<a href="catalog.php?model=<?=$row['id']?>"><?=$row['name']?></a>	
							</td>
						<tr>
					<?
				}
				echo "</table>";
			}
			exit;
		}

		// years
		if (!empty($_GET['catalog']))
		{
			$query = "SELECT * FROM year WHERE catalog = ".$_GET['catalog'];
			$result = mysql_query($query);

			if (mysql_num_rows($result))
			{
				echo "<table>";
				while ($row = mysql_fetch_assoc($result))
				{
					?>
						<tr>
							<td>
								<a href="catalog.php?year=<?=$row['id']?>"><?=$row['name']?></a>	
							</td>
						<tr>
					<?
				}
				echo "</table>";
			}
			exit;
		}

		// catalogs
		if (!empty($_GET['section']))
		{
			$query = "SELECT * FROM catalog WHERE section = ".$_GET['section'];
			$result = mysql_query($query);

			
			if (mysql_num_rows($result))
			{
				echo "<table>";
				while ($row = mysql_fetch_assoc($result))
				{
					?>
						<tr>
							<td>
								<a href="catalog.php?catalog=<?=$row['id']?>"><?=$row['name']?></a>	
							</td>
						<tr>
					<?
				}
				echo "</table>";
			}
			exit;
		}

		// sections
		$query = "SELECT * FROM section";
		$result = mysql_query($query);

		if (mysql_num_rows($result))
		{
			echo "<table>";
			while ($row = mysql_fetch_assoc($result))
			{
				?>
					<tr>
						<td>
							<a href="catalog.php?section=<?=$row['id']?>"><?=$row['name']?></a>
						</td>
					</tr>
				<?
			}
			echo "</table>";
		}
	}